// pages/diet/diet.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabCur: -1,
    tabNext: -1,
    scollLeft: 0,
    scollLeftNext: 0,
    btnFirst: ['低于500', '600-1000', '1100-1500', '1600-2000', '2100-2500', '2600-3000'],
    btnFirstValue: [500, 1000, 1500, 2000, 2500, 3000],
    btnNext: [],
    showNext: false,
  },

  tabSelect(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      tabCur: id,
      scrollLeft: (id-1)*60,
      tabNext: -1,
    })
    this.calNextRange(id)
  },
  tabSelectNext(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      tabNext: id,
      scrollLeftNext: (id-1)*60
    })
    let val = this.data.btnNext[id]
    this.showDietDetail(val);
  },

  // 计算子选项
  calNextRange(id) {
    let val = this.data.btnFirstValue[id];
    let res = []
    for (let i = 0; i < 10; i++) {
      res.push(val - i * 50)
    }
    this.setData({
      btnNext: res,
      showNext: true,
    })
  },

  showDietDetail(heat){
    wx.showLoading();
    var _this = this;
    let urlBase = getApp().globalData.urlBase
    _this.setData({
      dietDetailShow: true,
    })
    //请求后端数据
    wx.request({
      url: urlBase + '/wx/mealSetByCalorie',
      method:'POST',
      data:{
        min: heat - 50,
        max: heat + 50
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function(res){
        if (res.data != null && res.data.length > 0) {
          _this.setData({
            dietDetailShow: true,
            dietDetail: res.data,
            nullRecipe: false,
          })
        } else {
          _this.setData({
            dietDetail: [],
            nullRecipe: true
          })
        }
        wx.hideLoading()
      }
    })
  },
  toMealDetail(e) {
    // console.log(e.currentTarget.dataset.experienceid);
    let id = e.currentTarget.dataset.experienceid;
    wx.navigateTo({
      url: '../diet/mealsetDetail/detail?mid=' + id,
    })
  }
})